package com.cxlin.cwanandroid.ui.search.viewmodel

import androidx.lifecycle.viewModelScope
import com.cxlin.base.BaseViewModel
import com.cxlin.base.model.api.Result
import com.cxlin.base.mvi.UiEvent
import com.cxlin.base.mvi.UiIntent
import com.cxlin.base.mvi.UiState
import com.cxlin.cwanandroid.model.bean.ArticleList
import com.cxlin.cwanandroid.model.bean.Hot
import com.cxlin.cwanandroid.model.repository.CollectRepository
import com.cxlin.cwanandroid.model.repository.SearchRepository
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch

/**
 *
 * @Author: chenxunlin
 * @Date: 2024/5/11 14:53
 */
class SearchRecommendViewModel(
    private val searchRepository: SearchRepository,
    private val collectRepository: CollectRepository
) : BaseViewModel() {

    sealed class SearchUiState : UiState {
        object Idel : SearchUiState()
        data class IsRefresh(val result: Boolean) : SearchUiState()
        class Articles(val result: ArticleList?) : SearchUiState()
        class WebSites(val result: List<Hot>) : SearchUiState()
        class HotSearch(val result: List<Hot>) : SearchUiState()
        class ShowHot(val result: Boolean) : SearchUiState()
    }

    sealed class SearchEvent : UiEvent {
        class ToastEvent(val message: String, val short: Boolean = false) : SearchEvent()
    }

    sealed class SearchUiIntent : UiIntent {
        data class SearchHot(val key: String, val isRefresh: Boolean = false) : SearchUiIntent()
        data class CollectArticle(val articleId: Int, val boolean: Boolean) : SearchUiIntent()
        class GetWebSites() : SearchUiIntent()
        class GetHotSearch() : SearchUiIntent()
        class UpdateHotCotnentState(val showHot: Boolean) : SearchUiIntent()
    }

    private var currentPage = 0

    override fun handleIntent(intent: UiIntent) {
        when (intent) {
            is SearchUiIntent.SearchHot -> {
                viewModelScope.launch {
                    kotlin.runCatching {
                        if (intent.isRefresh) {
                            currentPage = 0
                            mUiStateFlow.update { SearchUiState.IsRefresh(false) }
                        }
                        val result = searchRepository.searchHot(currentPage, intent.key)
                        if (result is Result.Success) {
                            mUiStateFlow.update { SearchUiState.IsRefresh(false) }
                            mUiStateFlow.update { SearchUiState.Articles(result.data) }
                            mUiStateFlow.update { SearchUiState.ShowHot(false) }
                            currentPage += 1
                        } else if (result is Result.Error) {
                            mUiStateFlow.update { SearchUiState.IsRefresh(false) }
                            mEffectFlow.emit(SearchEvent.ToastEvent(result.exception.message ?: "网络异常", true))
                        }
                    }
                }
            }

            is SearchUiIntent.CollectArticle -> {
                viewModelScope.launch {
                    kotlin.runCatching {
                        if (intent.boolean) {
                            collectRepository.collectArticle(intent.articleId)
                        } else {
                            collectRepository.unCollectArticle(intent.articleId)
                        }

                    }
                }
            }

            is SearchUiIntent.GetHotSearch -> {
                viewModelScope.launch {
                    kotlin.runCatching {
                        val result = searchRepository.getHotSearch()
                        if (result is Result.Success) {
                            mUiStateFlow.update { SearchUiState.HotSearch(result.data) }
                        }
                    }
                }
            }

            is SearchUiIntent.GetWebSites -> {
                viewModelScope.launch {
                    kotlin.runCatching {
                        val result = searchRepository.getWebSites()
                        if (result is Result.Success) {
                            mUiStateFlow.update { SearchUiState.WebSites(result.data) }
                        }
                    }
                }
            }

            is SearchUiIntent.UpdateHotCotnentState -> {
                mUiStateFlow.update { SearchUiState.ShowHot(intent.showHot) }
            }
        }
    }

    override fun createInitState(): UiState {
        return SearchUiState.Idel
    }
}